<?php
/**
 * Created by PhpStorm.
 * User: zhangligang
 * Date: 17/5/8
 * Time: 下午1:21
 */

namespace App\Http\Controllers;

use App\Http\Models\Color;
use App\Http\Models\Goods;
use App\Http\Models\Product;
use App\Http\Presenter\GoodsPresenter;
use App\Http\Repository\GoodsRepository;
use App\Http\Service\GoodsService;
use Illuminate\Http\Request as HttpRequest;

class GoodsController extends BaseController
{

    /**
     * @param GoodsPresenter $presenter
     * @param GoodsRepository $repository
     * @param GoodsService $service
     */
    public function __construct(GoodsPresenter $presenter, GoodsRepository $repository, GoodsService $service)
    {

        $this->view = ['activeFirst' => 'goods', 'activeSecond' => 'index'];

        parent::__construct($presenter, $repository, $service);
    }

    /**
     * @return \Illuminate\View\View
     */
    public function index()
    {

        $goodsList = Goods::all(['id', 'price', 'storage', 'sale_num']);

        if (count($goodsList)) {

            $goodsList->load('product');
        }

        $this->view['activeSecond'] = 'index';

        return view('goods.index', array_merge(['goodsList' => $goodsList], $this->view));
    }

    public function create()
    {

        $goods = new Goods();

        $productList = Product::all(['id', 'short']);

        $colorList = Color::all(['id', 'name']);

        $this->view['activeSecond'] = 'create';

        return view('goods.edit', array_merge(['goods' => $goods, 'productList' => $productList, 'colorList' => $colorList], $this->view));
    }

    public function update($id)
    {

        $goods = Goods::with('product')->find($id);

        $productList = Product::all(['id', 'short']);

        $colorList = Color::all(['id', 'name']);

        $this->view['activeSecond'] = 'index';

        return view('goods.edit', array_merge(['goods' => $goods, 'productList' => $productList, 'colorList' => $colorList], $this->view));

    }

    public function save(HttpRequest $request)
    {

        $goodsArray = $request->all();

        if (empty($goodsArray['id'])) {

            $goods = new Goods();

        } else {

            $goods = Goods::query()->find($goodsArray['id']);
        }

        $goods->product_id = empty($goodsArray['product_id']) ? '' : $goodsArray['product_id'];
        $goods->spec = empty($goodsArray['spec']) ? '' : $goodsArray['spec'];
        $goods->unit = empty($goodsArray['unit']) ? '' : $goodsArray['unit'];
        $goods->value = empty($goodsArray['value']) ? '' : $goodsArray['value'];
        $goods->save();

        return redirect('goods.html');
    }

    public function delete(HttpRequest $request)
    {

        $id = (int)$request->get('id', 1);

        $code = 200;

        $message = '删除成功';

        if (Goods::query()->where('id', '=', $id)->delete() === false) {

            $code = 404;

            $message = '删除失败,请重试';
        }

        return $this->send($code, $message);
    }

    public function set(HttpRequest $request)
    {

    }

}